package com.lzc.springPart.aop.cglb;

import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 代理类  还会对应两个FastClass用于MethodProxy不使用反射进行原方法调用
 */
public class CglbImpl extends CglibProxyDemo.Cat {

    static Method eat;
    static MethodProxy eatProxy;
    static {
        try {
            eat = CglibProxyDemo.Cat.class.getDeclaredMethod("eat");
            // MethodProxy第一次调用是会创建两个FastClass代理对象，一个用于原始对象调用，一个用于代理对象调用，且与本代理类一一对应
            eatProxy = MethodProxy.create(CglibProxyDemo.Cat.class, CglbImpl.class,
                    "()V","eat","eatSuper");
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    private MyInvoke myInvoke;

    public CglbImpl(MyInvoke myInvoke){
        this.myInvoke = myInvoke;
    }

    //->>>>>>>>>>>>>>>>>>
    public void eatSuper(){
        super.eat();
    }

    @Override
    public void eat() {
        try {
            myInvoke.invoke(this,eat,null,eatProxy);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}
